// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
//广告配置文件
let adDataCenter =   cc.Class({
    extends: cc.Component,

    properties: {
        adData:null,
        isInited:false,
    },

    init(){
        if(this.isInited)
            return ;
        this.adData = {
            kp:{
                toolTip:"启动app全屏广告",
                type:"splash",
                panelName:"",
                codeId:"887297002"
            },
            lx:{
                toolTip:"离线收入银币激励广告",
                type:'reward',
                panelName:"offLine",
                codeId:"945038879"
            },
            hby_ex:{
                toolTip:"红包雨信息流广告",
                type:"express",
                panelName:"hb",
                codeId:"945038883",
            },
            hby_re:{
                toolTip:"红包雨激励视屏广告",
                type:"reward",
                panelName:"hb",
                codeId:"945038882"
            },
            sys_re_coin_re:{
                toolTip:'系统随机赠送的金币',
                type:"express",
                panelName:"sys",
                codeId:"945038893"
            },
            sys_re_coin_ex:{
                toolTip:"系统随机赠送的金币",
                type:"reward",
                panelName:"sys",
                codeId:"945038894"
            },
            sign_Tip_re:{
                toolTip:'签到弹窗',
                type:"reward",
                panelName:"sign",
                codeId:"945038901"
            },
            sign_Tip_ex:{
                toolTip:"签到弹窗",
                type:"express",
                panelName:"sign",
                codeId:"945038899"
            },
            sign_ex_1:{
                toolTip:"签到",
                type:"express",
                panelName:"sign",
                codeId:"945038907",
            },
            sign_ex_2:{
                toolTip:"签到",
                type:"express",
                panelName:"sign",
                codeId:"945038909",
            },
            task_ex:{
                toolTip:"每日任务",
                type:"express",
                panelName:"task",
                codeId:"945038926"
            },
            task_re:{
                toolTip:"每日任务",
                type:"reward",
                panelName:"task",
                codeId:"945038925"
            },
            task_com:{
                toolTip:"达标任务",
                type:"express",
                panelName:"task",
                codeId:"945038929"
            },
            tt_0:{
                toolTip:"大转盘没有奖励",
                type:"express",
                panelName:"tt",
                codeId:"945038932"
            },
            tt_1:{
                toolTip:"大转盘抽中金币",
                type:"express",
                panelName:"tt",
                codeId:"945038935",
            },
            tt_2_ex:{
                toolTip:"大转盘抽中银币",
                type:"express",
                panelName:"tt",
                codeId:"945038938",
            },
            tt_2_re:{
                toolTip:"大转盘抽中银币",
                type:"reward",
                panelName:"tt",
                codeId:"945039221"
            },
            tt_3:{
                toolTip:"大转盘抽中红包",
                type:"repress",
                panelName:"tt",
                codeId:"945038940"
            },
            tt_4:{
                toolTip:"大转盘-额外奖励",
                type:"express",
                panelName:"tt",
                codeId:"945038942"
            },
            tt_5:{
                toolTip:"大转盘-界面-静态广告",
                type:"express",
                panelName:"tt",
                codeId:"945038943"
            },
            ggk_0:{
                toolTip:"刮刮卡动态广告1",
                type:"express",
                panelName:"ggk",
                codeId:"945038950"
            },
            ggk_1:{
                toolTip:"刮刮卡动态广告2",
                type:"express",
                panelName:"ggk",
                codeId:"945038951",
            },
            ggK_3:{
                toolTip:"刮刮卡-挂中（银币、金币-2倍，红包-不双倍）",
                type:"express",
                panelName:"ggk",
                codeId:"945038953"
            },
            ggk_4:{
                toolTip:"刮刮卡-挂中（银币、金币-2倍，红包-不双倍）",
                type:"reward",
                panelName:"ggk",
                codeId:'945038956'
            },
            game_update_0:{
                toolTip:"游戏区合并升级新的蛋糕",
                type:"reward",
                panelName:"game",
                codeId:"945038962"
            },
            game_update_1:{
                toolTip:'游戏区随机跨级获得蛋糕',
                type:"reward",
                panelName:"game",
                codeId:"945038964",
            },
            game_update_2:{
                toolTip:"游戏区合并时随机获得体验分红蛋糕",
                type:"reward",
                panelName:"game",
                codeId:"945039063"
            },
            my_info:{
                toolTip:"我的界面静态广告",
                type:"express",
                panelName:"myInfo",
                codeId:"945039083"
            },
            coin:{
                toolTip:"金币兑换现金",
                type:"reward",
                panelName:"coin",
                codeId:'945039084',
            }
        }
        this.isInited = true;
    },

    toShowAd(key,isHide = false){
        if(!this.isInited)
            this.init();
        let data = this.adData[key];
        if(!data)
        {
            cc.error("invalid key",key);
            return ;
        }
        if(CC_JSB)
        {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity","onGetAdInfo","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V",data.type,data.codeId,data.panelName,isHide);
        }
        else
        {
            cc.log("platform error");
            if(key == "kp")
            {
                cc.tg.jcj.onSplashAdOver();
            }
           // 
        }
    },
    wxLogin(){
        if(CC_JSB)
        {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity","wxLogin","()V",);
        }
        else
        {
            cc.log("platform error");
        }
    },
    //文本复制
   copy(str) {
       if(cc.sys.isBrowser)
       {
           // web 平台ok
            var save = function (e) {
                e.clipboardData.setData('text/plain', str);
                e.preventDefault();
            }
            document.addEventListener('copy', save);
            document.execCommand('copy');
            document.removeEventListener('copy', save);
       }
       else if(cc.sys.platform == cc.sys.OS_ANDROID)
       {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity","onCopy","(Ljava/lang/String;)V",str);
       }
       else if(cc.sys.platform == cc.sys.OS_IOS)
       {
            // ios平台复制
       }
      
   },


   createQR(node,url){
    if(!node || !url)
    {
        cc.log("invalid node or url",node,url);
        return ;
    }
    let QRCode = require("qrcode");
    var qrcode = new QRCode(-1, 2);
		qrcode.addData(url);
        qrcode.make();
    	let size = node.width;
		let num = qrcode.getModuleCount();
		var ctx = node.getComponent(cc.Graphics);
		ctx.clear();
		ctx.fillColor = cc.Color.BLACK;
		// compute tileW/tileH based on node width and height
		var tileW = size / num;
		var tileH = size / num;
		// draw in the Graphics
		for (var row = 0; row < num; row++) {
			for (var col = 0; col < num; col++) {
				if (qrcode.isDark(row, col)) {
					var w = (Math.ceil((col + 1) * tileW) - Math.floor(col * tileW));
					var h = (Math.ceil((row + 1) * tileW) - Math.floor(row * tileW));
					ctx.rect(Math.round(col * tileW), size - tileH - Math.round(row * tileH), w, h);
					ctx.fill();
				} else {
					// ctx.fillColor = cc.Color.WHITE;
				}
			}
		}
   }

});

module.exports = adDataCenter;
